package com.felix.netty.c1.handler;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

/**
 * @author felix
 * @date 2025/5/22 11:00
 *
 *
 * 入站出站注册顺序为：in1->in2->out1->in3->out2->out3
 *
 * ctx.fireChannelRead(msg);从当前位置向后找下一个入站处理器
 * ctx.channel().write(msg,promise);是Channel上的write，从Pipeline尾部开始往前找出站处理器
 * ctx.write(msg,promise);是context上的write，从当前位置向前找下一个出站处理器
 *
 */
public class Server {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("in1");
                                ctx.fireChannelRead(msg);
                            }
                        });

                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("in2");
                                ctx.fireChannelRead(msg);
                            }
                        });

                        ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("out1");
                                ctx.channel().write(msg,promise);
                            }
                        });

                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("in3");
                                ctx.write(msg);
                            }
                        });


                        ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("out2");
//                                ctx.write(msg,promise);
                            }
                        });

                        ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("out3");
                                ctx.write(msg,promise);
                            }
                        });

                    }
                })
                .bind(8080);
    }

}
